import { BASE_URL } from '../../apis/requests.js';
// pages/getBooksBy/getBooksBy.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    picker: ["书名", "种类", "出版社", "作者", "全部"], //与index联系
    index: 0,
    searchName: "",
    books: [],
  },

  bindUpdateDel(e) {
    console.log(e.currentTarget.dataset)

    const values = {
      "token": wx.getStorageSync('token'),
      "book_id": this.data.books[e.currentTarget.dataset.index].book_id,
    }


    wx.request({
      url: BASE_URL + "/admin/delete",
      data: values,
      header: {
        'content-type': 'application/json'
      },
      method: 'POST',
      dataType: 'json',
      success: (result) => {
        console.log(result)
        if (result.data.code === 200) {
          wx.showModal({
            title: "提示",
            content: "删除成功",
            showCancel: false,
            success(res) {
              wx.navigateBack({
                complete: (res) => {},
              })
            }
          })
        } else {
          wx.showModal({
            title: "提示",
            content: result.data.msg,
            showCancel: false,
            success(res) {
            }
          })
        }
      
      },
      fail: () => {},
      complete: () => {}
    })

    // wx.navigateTo({
    //   url: './updateDel/updateDel?book_id=' + this.data.books[e.currentTarget.dataset.index].book_id,
    // })

  },

  PickerChange(e) {
    this.setData({
      index: parseInt(e.detail.value)
    })
  },

  inputChange(e) {
    this.setData({
      searchName: e.detail.value
    })
  },

  search() {
    const values = {
      "token": wx.getStorageSync('token'),
      "type": this.data.index,
       "info": this.data.searchName
    }

    // console.log("回车")
    if (this.data.searchName.length === 0) {
      return
    }
    wx.request({
      url: BASE_URL + "/admin/find_books_by",
      data: values,
      header: {
        'content-type': 'application/json'
      },
      method: 'POST',
      dataType: 'json',
      success: (result) => {
        console.log(result)
        if (result.data.code === "200") {
          this.setData({
            books: result.data.data
          })
        } else {
          wx.showModal({
            title: "提示",
            content: "查询失败，请重试！",
            showCancel: false,
            success(res) {
            }
          })
        }
      
      },
      fail: () => {},
      complete: () => {}
    })

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})